package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.sky.anno.AutoFill;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class setmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    @AutoFill(OperationType.INSERT)
    public void add(SetmealDTO setmealDTO) {
        // 插入优惠劵表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);

        // 插入优惠卷关联菜品表
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(item -> {
            item.setSetmealId(id);
        });
        setmealMapper.insertSetmealDishes(setmealDishes);
    }

    @Override
    public PageResult queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        Page<SetmealVO> page = setmealMapper.queryPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public SetmealVO getById(Long id) {
        // 查询优惠券
        SetmealVO setmealVO = setmealMapper.getBySetmealId(id);

        // 查询关联的菜品
        List<SetmealDish> setmealDishes = setmealMapper.getBySetmealIdForDishes(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public boolean isStatus(List<Long> ids) {
        List<Setmeal> setmealList = setmealMapper.getByIds(ids);
        for(Setmeal setmeal: setmealList) {
            if (setmeal.getStatus() == 1)
                return false;
        }
        return true;
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        // 删除优惠券中的优惠
        setmealMapper.deleteSetmealByIds(ids);

        // 删除关联表中关联
        setmealMapper.deleteSetmealByIds(ids);
    }

    @Transactional
    @Override
    public void put(SetmealDTO setmealDTO) {
        // 修改优惠券表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 删除相关的菜品关联
        Long id = setmeal.getId();
        setmealMapper.deleteDishesById(id);

        // 插入菜品关联
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(item -> {
            item.setSetmealId(id);
        });
        setmealMapper.insertSetmealDishes(setmealDishes);
    }

    @Override
    public void setStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    @Override
    public boolean isAllDishesStatus(Long id) {
        // 查找关联的dish_ids
        List<Long> longs = setmealMapper.queryDishIds(id);

        // 查找所有关联的所有dish
        for(Long idx: longs) {
            if(dishMapper.getById(idx).getStatus() == 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
